<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_exception_ErrorHandler
{
	const PROD = 0;
	const DEVELOPMENT = 100;
	const STRICT = 200;
	
	public static function errorHandler($errno, $errmsg, $filename, $linenum, $vars)
	{
		// If error is generated by a function with @
        if(error_reporting() == 0) return false;
		        
		$parameters = f_loader_Config::getInstance()->getParameters("default");
		$logger = f_logs_Logger::getInstance();

        error_reporting(E_ALL ^ E_NOTICE);

		$errorLevel = self::STRICT;

		if(isset($parameters['errorLevel']) && defined('self::'.strtoupper($parameters['errorLevel'])))
		{
			$errorLevel = constant('self::'.strtoupper($parameters['errorLevel']));
		}

		switch ($errno)
		{
		    case E_NOTICE:
                if($errorLevel > self::DEVELOPMENT)
		    	{
                    echo '<pre>'.$filename." (ln.".$linenum.") - ".$errmsg.'</pre>';                    
                }
		    	$logger->noticeLog($filename." (ln.".$linenum.") - ".$errmsg);	    	
		    break;
		    
		    default:
		    	if($errorLevel > 100)
		    	{
		    		$logger->errorLog($filename." (ln.".$linenum.") - ".$errmsg);
		    		$error = new ErrorException($errmsg, 5, 0, $filename, $linenum);
		    		f_exception_BaseException::exceptionHandler($error, $errno);
		    	}
		    break;
		}

        return false;
	}
	
	public static function cliErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
	{
		echo COLOR_RED."[ERROR]".COLOR_NORMAL." -> ".$errmsg." in ".$filename." line ".$linenum.COLOR_NORMAL."\n";
		die;
	}	
}